package 优先算法.滑动窗口.长度最小的子数组;

/**
 * <a href="https://leetcode.cn/problems/minimum-size-subarray-sum/description/"
 */
public class MinArray {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] array = {2,3,1,2,4,3};
        int target = 7;
        int rel = so.minSubArrayLen(target, array);
        System.out.println(rel);
    }
}
class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int left=0,right=0,len=Integer.MAX_VALUE,sum=0;

        while(left < nums.length){
            //进窗口
            while (sum < target && right < nums.length) {
                sum += nums[right++];
            }
            //出窗口 更新len
            if (sum>=target) {
                len =Math.min(len,right - left);
            }
            sum -= nums[left++];
        }
        return len==Integer.MAX_VALUE? 0 : len;
    }
}